home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmstring.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  9.1 KB  |  178 lines

  1. // CmString.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Character string definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMSTRING_H
  10. #define _CMSTRING_H
  11.  
  12. #include <cm/include/cmobject.h>
  13.  
  14. class CmSubString;                                // Substring class stub.
  15.  
  16. class CmString : public CmObject {                // String class definition.
  17. public:
  18.   CmString(const char* = NULL);                   // Default constructor.
  19.   CmString(int, char);                            // Construct with character.
  20.   CmString(const CmSubString&);
  21.   CmString(const CmString&);                      // Copy constructor.
  22.  ~CmString();                                     // Destructor.
  23.  
  24.   CmString& operator=(const CmString&);           // Assignment operator.
  25.   CmString& operator=(const char*);               // Assignment operator.
  26.  
  27.   char& operator[](int);                          // Return char at position.
  28.   char  operator[](int) const;                    // Return char at position.
  29.  
  30.         CmSubString operator()(int, int);         // Substring operator.
  31.   const CmSubString operator()(int, int) const;   // SubString operator.
  32.  
  33.   operator const char*() const;                   // Type conversion op.
  34.  
  35.   char*    text        () const;                  // Return char pointer.
  36.   int      length      () const;                  // Return string length.
  37.   int      index       (char) const;              // Return first occurrence.
  38.   void     leftJustify ();                        // Left justify string.
  39.   void     rightJustify();                        // Right justify string.
  40.   void     trim        ();                        // Trim lead/trail blanks.
  41.   void     toLower     ();                        // Lower case entire string.
  42.   void     toUpper     ();                        // Upper case entire string.
  43.   Bool     isEqual     (CmObject*) const;         // Compare objects.
  44.   int      compare     (CmObject*) const;         // Compare objects.
  45.   unsigned hash        (unsigned)  const;         // String hash function.
  46.   void     printOn     (ostream&)  const;         // Print string to stream.
  47.   void     readFrom    (istream&);                // Read string from stream.
  48.   Bool     write       (CmReserveFile&) const;    // Write string to file.
  49.   Bool     read        (CmReserveFile&);          // Read string from file.
  50.  
  51.   CMOBJECT_DEFINE(CmString, CmObject)             // Define object funcs.
  52.  
  53.   Bool      operator< (const CmString&) const;    // Check if input <  this.
  54.   Bool      operator<=(const CmString&) const;    // Check if input <= this.
  55.   Bool      operator> (const CmString&) const;    // Check if input >  this.
  56.   Bool      operator>=(const CmString&) const;    // Check if input >= this.
  57.   Bool      operator==(const CmString&) const;    // Check if input == this.
  58.   Bool      operator!=(const CmString&) const;    // Check if input != this.
  59.   CmString  operator+ (const CmString&) const;    // Append input to string.
  60.   CmString& operator+=(const CmString&);          // Append input to string.
  61.  
  62.   Bool      operator< (const char*) const;        // Check if input <  this.
  63.   Bool      operator<=(const char*) const;        // Check if input <= this.
  64.   Bool      operator> (const char*) const;        // Check if input >  this.
  65.   Bool      operator>=(const char*) const;        // Check if input >= this.
  66.   Bool      operator==(const char*) const;        // Check if input == this.
  67.   Bool      operator!=(const char*) const;        // Check if input != this.
  68.   CmString  operator+ (const char*) const;        // Append input to string.
  69.   CmString& operator+=(const char*);              // Append input to string.
  70.  
  71.   friend Bool     operator< (const char*, const CmString&); // Check if A <  B.
  72.   friend Bool     operator<=(const char*, const CmString&); // Check if A <= B.
  73.   friend Bool     operator> (const char*, const CmString&); // Check if A >  B.
  74.   friend Bool     operator>=(const char*, const CmString&); // Check if A >= B.
  75.   friend Bool     operator==(const char*, const CmString&); // Check if A == B.
  76.   friend Bool     operator!=(const char*, const CmString&); // Check if A != B.
  77.   friend CmString operator+ (const char*, const CmString&); // Append B to A.
  78.  
  79.   static void caseSensitive(Bool);                // Set case sensitive compare.
  80.   static Bool caseSensitive();                    // Get case sensitive compare.
  81.  
  82. protected:
  83.   void copy  (const char*);                       // Copy chars into string.
  84.   void append(const CmString &AString);           // Append chars to string.
  85.  
  86.   char       *_text;                              // Character array.
  87.   static Bool _caseSensitive;                     // Case sensitive compare.
  88.   friend      CmSubString;                        // Substring can access.
  89. };
  90.  
  91. class CmSubString {                               // Sub-string definition.
  92. public:
  93.   CmString& operator=(const CmString&);           // Assignment from string.
  94.   CmString& operator=(const CmSubString&);        // Assignment from sub-str.
  95.   CmString& operator=(const char*);               // Assignment from char*.
  96.  
  97.   operator const char*() const;                   // Type conversion.
  98.  
  99.   Bool operator< (const CmString&) const;         // See if input <  this.
  100.   Bool operator<=(const CmString&) const;         // See if input <= this.
  101.   Bool operator> (const CmString&) const;         // See if input >  this.
  102.   Bool operator>=(const CmString&) const;         // See if input >= this.
  103.   Bool operator==(const CmString&) const;         // See if input == this.
  104.   Bool operator!=(const CmString&) const;         // See if input != this.
  105.  
  106.   Bool operator< (const CmSubString&) const;      // See if input <  this.
  107.   Bool operator<=(const CmSubString&) const;      // See if input <= this.
  108.   Bool operator> (const CmSubString&) const;      // See if input >  this.
  109.   Bool operator>=(const CmSubString&) const;      // See if input >= this.
  110.   Bool operator==(const CmSubString&) const;      // See if input == this.
  111.   Bool operator!=(const CmSubString&) const;      // See if input != this.
  112.  
  113.   Bool operator< (const char*) const;             // See if input <  this.
  114.   Bool operator<=(const char*) const;             // See if input <= this.
  115.   Bool operator> (const char*) const;             // See if input >  this.
  116.   Bool operator>=(const char*) const;             // See if input >= this.
  117.   Bool operator==(const char*) const;             // See if input == this.
  118.   Bool operator!=(const char*) const;             // See if input != this.
  119.  
  120.   friend Bool operator< (const char*, const CmSubString&); // See if A <  B.
  121.   friend Bool operator<=(const char*, const CmSubString&); // See if A <= B.
  122.   friend Bool operator> (const char*, const CmSubString&); // See if A >  B.
  123.   friend Bool operator>=(const char*, const CmSubString&); // See if A >= B.
  124.   friend Bool operator==(const char*, const CmSubString&); // See if A == B.
  125.   friend Bool operator!=(const char*, const CmSubString&); // See if A != B.
  126.  
  127.   friend ostream& operator<<(ostream&, const CmSubString&);  // Stream output.
  128.  
  129. private:
  130.   CmSubString(const CmString&, int, int);         // Sub-string constructor.
  131.   CmSubString(const CmSubString&);                // Copy constructor.
  132.  
  133.   void replace(const char*, int);                 // Replace sub-str contents.
  134.  
  135.   char     *_sp;                                  // Sub string text.
  136.   int       _sl;                                  // Length of sub string.
  137.   CmString *_st;                                  // String this is sub of.
  138.   friend    CmString;                             // String can access.
  139. };
  140.  
  141. // "=" operator sets the contents of this string to that of an input string.
  142. inline CmString& CmString::operator=(const CmString &AString)
  143. { if (&AString != this) copy(AString._text); return *this; }
  144.  
  145. // "=" operator sets the contents of this string to that of an input string.
  146. inline CmString& CmString::operator=(const char* s)
  147. { if (s != _text) copy(s); return *this; }
  148.  
  149. // "[]" operator returns the character at a given index.
  150. inline char& CmString::operator[](int idx)
  151. { return _text[idx]; }
  152.  
  153. // "[]" operator returns the character at a given index.
  154. inline char CmString::operator[](int idx) const
  155. { return _text[idx]; }
  156.  
  157. // "operator const char*" returns the character pointer for this string.
  158. inline CmString::operator const char*() const
  159. { return _text; }
  160.  
  161. // "text" returns the character pointer for this string.
  162. inline char* CmString::text() const
  163. { return _text; }
  164.  
  165. // "length" returns the length of the character string.
  166. inline int CmString::length() const
  167. { return (_text) ? strlen(_text) : 0; }
  168.  
  169. // "printOn" prints the string to the specified output stream.
  170. inline void CmString::printOn(ostream& os) const
  171. { os << _text; }
  172.  
  173. // "operator const char*" returns the text pointer of this sub string.
  174. inline CmSubString::operator const char*() const
  175. { return _sp; }
  176.  
  177. #endif
  178.